package com.toobigtofail.enitities
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.Joints.b2RevoluteJointDef;
	import Box2D.Dynamics.b2Body;
	
	import com.toobigtofail.shapes.IShape;
	import com.toobigtofail.shapes.Wedge;
	
	import fabulaexmachina.box2fp.Box2DEntity;
	import fabulaexmachina.box2fp.Box2DWorld;
	import fabulaexmachina.box2fp.graphics.SuperGraphiclist;
	
	import net.flashpunk.FP;
	import net.flashpunk.Graphic;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.PreRotation;
	
	public class Enemy extends Box2DEntity
	{
		[Embed(source="/assets/gfx/spikes.png")] private static const SPIKES_IMG:Class;
		[Embed(source="/assets/gfx/spikesrev.png")] private static const SPIKESREV_IMG:Class;

		public static const TYPE:String ="spike";
		
		private var _shape:IShape;
		
		private var _speed:Number = 1;
		
		private var _initOptions:Object;
		
		public function Enemy(shape:IShape, angleRadicals:Number=0, flippin:Boolean=false)
		{
		
			_shape = shape;
			_initOptions = {angleRads: angleRadicals,
							flip: flippin}
			super(shape.X, shape.Y, shape.Height, shape.b2Type);
			var img:PreRotation;
			if(_initOptions.flip) 
			{
				img = new PreRotation(SPIKESREV_IMG);	
			}
			else
			{
				img = new PreRotation(SPIKES_IMG);
			}
			img.originX = 0;
			img.originY =0;
			//img.centerOrigin();
			img.angle = 180 * (angleRadicals/Math.PI);
			img.x += 24;
			img.y += 10;
			
			if(img.angle > -90)
			{
				img.x += 0;
				img.y -=20;
			}
			//if(flippin) img.flipped = true;
			this.addGraphic(img);
			//img.scale = _shape.Width*(FP.world as Box2DWorld).scale/img.width;
			//img.scale = (_shape.Width*(FP.world as Box2DWorld).scale)/img.width;
			type = Enemy.TYPE;
			
			
			

		}
		
		override public function added():void
		{
			super.added();
			/*var spikeJointDef:b2RevoluteJointDef = new b2RevoluteJointDef;
			spikeJointDef.Initialize(this.body, _initOptions.cavern.body, this.body.GetWorldCenter());*/
			body.SetLinearVelocity(new b2Vec2(0, -_speed));
			//angleRads = _initOptions.angleRads;
		}
		override public function buildShapes(friction:Number, 
											 density:Number, restitution:Number,
											 group:int, category:int, collmask:int):void { 
			_shape.buildShape(body);
			body.SetType(_shape.b2Type);
		}
		
		public function set speed(s:Number):void
		{
			_speed = s;
			if (body) body.SetLinearVelocity(new b2Vec2(0,-_speed));
		}
		public function get speed():Number { return _speed; }
		
		override public function update():void
		{
			super.update();
			if(this.body.GetWorldCenter().y < -20)
			{
				this.remove();
			}
		}
	}
}